package com.dingwen.stusprbooes.util;

import com.dingwen.stusprbooes.constant.CrawlConstant;
import com.dingwen.stusprbooes.entity.GoodsEntity;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 爬取数据工具
 *
 * @author dingwen
 * 2021/6/27 17:17
 **/
public class CrawlUtil {


    /**
     * 爬
     *
     * @return {@link List<GoodsEntity>}
     */
    public static List<GoodsEntity> crawl() {
        List<GoodsEntity> goodsEntityList = new ArrayList<>();
        CrawlConstant.KEY_LIST.forEach(keyword -> {
            try {
                goodsEntityList.addAll(getData(keyword));
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
        return goodsEntityList;
    }

    public static List<GoodsEntity> getData(String keyword) throws IOException {
        List<GoodsEntity> goodsEntityList = new ArrayList<>();
        // 需要爬取商品信息的网站地址
        String url = CrawlConstant.URL + keyword;
        // 动态模拟请求数据
        HttpGet httpGet = new HttpGet(url);
        CloseableHttpClient httpclient = HttpClients.createDefault();

        httpGet.setHeader("user-agent", CrawlConstant.USER_AGENT);
        CloseableHttpResponse response = httpclient.execute(httpGet);
        // 获取响应状态码
        int statusCode = response.getStatusLine().getStatusCode();

        HttpEntity entity = response.getEntity();
        if (statusCode == HttpStatus.SC_OK) {
            String html = EntityUtils.toString(entity, Consts.UTF_8);
            Document document = null;
            document = Jsoup.parse(html);
            Elements elements = document.select("div[class='view grid-nosku']");
            Elements elementsLi = elements.select("div[class='product']");
            elementsLi.forEach(li -> {
                // 商品ID
                String id = li.select("div[class='product']").select("p[class='productStatus']").select("span[class='ww-light ww-small m_wangwang J_WangWang']").attr("data-item");
                // 商品名称
                String name = li.select("p[class='productTitle']").select("a").attr("title");
                // 商品价格
                String price = li.select("p[class='productPrice']").select("em").attr("title");
                goodsEntityList.add(GoodsEntity.builder().id(id).name(name).prices(Double.parseDouble(price)).build());
            });
        }
        EntityUtils.consume(response.getEntity());
        response.close();
        return goodsEntityList;
    }
}
